<template>
<div class="container">
    <h1 class="title">请提交bug相关信息</h1>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item label="bug名称：" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="提交给：" prop="name">
            <el-select v-model="value" placeholder="请选择" @change="change">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="上传图片：" required>
            <el-row>
                <el-upload
                    class="avatar-uploader"
                    action="http://localhost:8080/"
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
                <el-button @click="upLoad">
                上传bug图(选择完图片，务必点击上传)
                </el-button>
            </el-row>
        </el-form-item>
        <el-form-item label="bug描述：" prop="desc">
            <el-input type="textarea" v-model="ruleForm.desc" rows="10"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="transBug">提交bug</el-button>
        </el-form-item>
    </el-form>
</div>
</template>
<script>
import axios from 'axios'
import { mapGetters } from 'vuex'
const ERR_OK = 1
export default {
  data() {
    return {
      ruleForm: {
        name: '',
        desc: ''
      },
      rules: {
        name: [{ required: true, message: '请输入bug名称', trigger: 'blur' }],
        desc: [{ required: true, message: '请填写bug描述', trigger: 'blur' }]
      },
      options: [],
      value: '',
      huser_id: 0,
      // 上传图片
      imageUrl: '',
      downLoadUrl: '',
      file: {},
      huser_id: 0
    }
  },
  created() {
    this._getFUserData()
    let msg = localStorage.getItem('loginMsg')
    this.huser_id = JSON.parse(msg).huser_id
  },
  methods: {
    //上传图片
    upLoad() {
      if (!this.imageUrl) {
        this.$notify({
          title: '提示',
          message: '请先添加缩略图！',
          position: 'top-left'
        })
        return false
      }
      const url = 'api/bugs/uploadPic.html'
      const param = {
        pic: this.imageUrl,
        pic_name: this.file.name
      }
      axios
        .post(url, param)
        .then(res => {
          let data = res.data
          if (data.code === ERR_OK) {
            this.downLoadUrl = data.data.imgPath
            this.$notify({
              title: '成功',
              message: res.message,
              position: 'top-left',
              type: 'success'
            })
          } else {
            this.$notify({
              title: '失败',
              message: res.message,
              position: 'top-left',
              type: 'error'
            })
          }
        })
        .catch(e => e)
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      let that = this
      const isJPG = file.type === 'image/jpeg'
      const isLt = file.size / 1024 / 1024 < 0.5
      var reader = new FileReader()
      this.file = file
      reader.readAsDataURL(file)
      reader.onload = function() {
        //读取完成后，数据保存在对象的result属性中
        that.imageUrl = this.result
      }

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt) {
        this.$message.error('上传头像图片大小不能超过 0.5MB!')
      }
      return false
    },
    _getFUserData() {
      const url = 'api/huser/huserList.html'
      axios
        .post(url)
        .then(res => {
          if (parseInt(res.data.code) === ERR_OK) {
            this.options = this.toOptions(res.data.data)
          }
        })
        .catch(e => {
          console.log(e)
        })
    },
    //表单提交函数组
    change(val) {
      localStorage.setItem('bugs_suid', val)
    },
    toOptions(obj) {
      let row = []
      for (let i = 0, len = obj.length; i < len; i++) {
        let ret = {
          value: obj[i].huser_id,
          label: obj[i].huser_name
        }
        row.push(ret)
      }
      return row
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    transBug() {
      const url = 'api/bugs/bugsadd.html'
      const bugData = {
        bugs_uid: this.huser_id,
        bugs_pid: 1,
        bugs_title: this.ruleForm.name,
        bugs_description: this.ruleForm.desc,
        bugs_content: '1212111',
        bugs_suid: this.value,
        bugs_img: this.downLoadUrl
      }
      axios
        .post(url, bugData)
        .then(res => {
          if (res.data.code === ERR_OK) {
            this.$router.push('/bugs')
            this.$notify({
              title: '提示',
              message: res.data.msg,
              position: 'top-left',
              type: 'success'
            })
          } else {
            this.$notify({
              title: '提示',
              message: res.data.msg,
              position: 'top-left',
              type: 'error'
            })
          }
        })
        .catch(e => {
          console.log(e)
        })
    }
  },
  computed: {
    ...mapGetters(['person'])
  }
}
</script>
<style lang="stylus" scoped rel="stylesheet/stylus">
.container
    width 600px
    margin 0 auto
    padding 60px
    .title
        letter-spacing 5px
    .demo-ruleForm
        padding-top 40px
.avatar-uploader .el-upload
    border 1px dashed #d9d9d9
    border-radius 6px
    cursor pointer
    position relative
    overflow hidden
    width 200px
    height 200px
.avatar-uploader .el-upload:hover
    border-color #409eff
.avatar-uploader-icon
    font-size 28px
    color #8c939d
    width 178px
    height 178px
    line-height 178px
    text-align center
.avatar
    width 178px
    height 178px
    display block
</style>
